import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import type { UserInforT, LoginT } from "~/UserInfor"
import request from "@/network/request"



export const useUserStore = defineStore('user', () => {
    const userInfor = ref<UserInforT>({})

    let loginFn = (data: LoginT) => {
        return new Promise(async resolve => {
            let res=await request({
                url: "/user/login",
                method: "post",
                params: data,
            })
            console.log("请求结果：",res);
            // 1.把用户信息存入仓库
            userInfor.value=res.data[0];
            // 把用户信息进行本地存储，防止页面刷新，数据丢失
            sessionStorage.setItem("userInfor",JSON.stringify(res.data[0]));
            // 2.把token信息存在本地
            localStorage.setItem("token",res.token);
            // 3.调用resolve函数，才能在外面触发then方法
            resolve("");
            
        })


    }

    return { userInfor, loginFn }
})
